package com.gw.quotes.activity.fragment;

import java.util.List;

import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gw.quotes.R;
import com.gw.quotes.activity.PriceInfoActivity;
import com.gw.quotes.adapter.Goods5Adapter;
import com.gw.quotes.entity.business.GetOrdersRecords;
import com.gw.quotes.entity.business.GetOrdersRecords.OrdersRecords;
import com.gw.quotes.entity.business.Priceinfo;
import com.gw.quotes.net.BaseServiceInter;
import com.gw.quotes.net.HttpCallBack;
import com.gw.quotes.net.Request;
import com.gw.quotes.net.Response;
import com.gw.quotes.net.service.OrdersRecordsGetServiceImpl;
import com.gw.quotes.util.CommonTools;
import com.yxw.view.NoScrollListView;
import com.yxw.view.YXWProgressDialog;
import com.yxw.view.YXWProgressDialog.YXWDialog;

/**
 * 
 * 
 */
public class GoodsFragment5 extends Fragment {

	private View contextView = null;
	private Goods5Adapter goodsAdapter;
	private GetOrdersRecords getOrdersRecords;
	/**
	 * 成交记录
	 */
	private List<OrdersRecords> ordersRecordsList;
	private YXWDialog dialog;
	private int pageNo;
	private int allPage;

	@ViewInject(id = R.id.listview)
	private NoScrollListView listview;
	@ViewInject(id = R.id.no_result_layout)
	private RelativeLayout no_result_layout;
	@ViewInject(id = R.id.goods5_page_layout)
	private LinearLayout pageLayout;
	@ViewInject(id = R.id.page_previous)
	private Button previous;
	@ViewInject(id = R.id.page_next)
	private Button next;
	@ViewInject(id = R.id.page_all)
	private TextView all;
	@ViewInject(id = R.id.page_cur)
	private TextView cur;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		if (contextView == null) {
			contextView = inflater.inflate(R.layout.fragment_activity_goods5,
					null);
		}
		ViewGroup parent = (ViewGroup) contextView.getParent();
		if (parent != null) {
			parent.removeView(contextView);
		}
		pageNo = 1;
		FinalActivity.initInjectedView(this, contextView);
		getOrdersRecords();
		return contextView;
	}

	private void initView() {
		ordersRecordsList = getOrdersRecords.getOrdersrecords();
		if (ordersRecordsList == null || ordersRecordsList.size() == 0) {
			listview.setVisibility(View.GONE);
			no_result_layout.setVisibility(View.VISIBLE);
		} else {
			goodsAdapter = new Goods5Adapter(getActivity(),
					R.layout.item_goods5, ordersRecordsList);
			listview.setAdapter(goodsAdapter);
			goodsAdapter.notifyDataSetChanged();
			listview.setVisibility(View.VISIBLE);
			no_result_layout.setVisibility(View.GONE);
			pageLayout.setVisibility(View.VISIBLE);

			pageNo = getOrdersRecords.getPageno().intValue();
			allPage = getOrdersRecords.getTotalrows().intValue();

			if (allPage % 10 == 0) {

				allPage = allPage / 10;

			} else {

				allPage = (allPage / 10) + 1;
			}

			all.setText("共" + allPage + "页");
			cur.setText("第" + pageNo + "页");
			GoodsF5OnclickListner onclick = new GoodsF5OnclickListner();
			if (pageNo <= 1) {

				previous.setEnabled(false);
				// previous.setVisibility(View.GONE);
			} else {

				previous.setEnabled(true);
				// previous.setVisibility(View.VISIBLE);
			}
			previous.setOnClickListener(onclick);
			if (pageNo >= allPage) {

				next.setEnabled(false);
				// next.setVisibility(View.GONE);
			} else {

				next.setEnabled(true);
				// next.setVisibility(View.VISIBLE);
			}
			next.setOnClickListener(onclick);
		}
	}

	public void getOrdersRecords() {
		Priceinfo priceinfo = ((PriceInfoActivity) getActivity())
				.getPriceinfo();
		dialog = new YXWProgressDialog(getActivity()).createDialog("加载中...");
		dialog.show();
		BaseServiceInter httpService = new OrdersRecordsGetServiceImpl();
		Request request = new Request();
		request.setGoodscode(priceinfo.getGoodscode());
		request.setPageNo(pageNo);
		Response response = httpService.handler(request,
				new HttpCallBack<Response<GetOrdersRecords>>() {
					@Override
					public void onFinished(Response<GetOrdersRecords> response) {
						dialog.dismiss();
						if (response.getResult() == 1) {
							GetOrdersRecords or = response.getObj();
							setGetOrdersRecords(or);
							initView();
						} else {
							Toast.makeText(getActivity(), "网络请求失败！", 1).show();
						}
					}
				});
	}

	public void setGetOrdersRecords(GetOrdersRecords getOrdersRecords) {
		this.getOrdersRecords = getOrdersRecords;
	}

	public class GoodsF5OnclickListner implements OnClickListener {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.page_previous:
				pageNo -= 1;
				getOrdersRecords();
				break;
			case R.id.page_next:
				pageNo += 1;
				getOrdersRecords();
				break;
			default:
				break;
			}
		}

	}
}
